package net.ddns.xcyyds.kungfu.procedures;

import net.minecraftforge.registries.ForgeRegistries;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.EntityDamageSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.sounds.SoundSource;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.core.BlockPos;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;
import net.ddns.xcyyds.kungfu.init.KungFuModEntities;
import net.ddns.xcyyds.kungfu.entity.LLJianQiEntity;

import java.util.stream.Collectors;
import java.util.List;
import java.util.Comparator;

public class LLProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		double a = 0;
		double NL = 0;
		if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new KungFuModVariables.PlayerVariables())).oneHandJianLL) {
			NL = 4;
			if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
					.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= NL) {
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).neiLi - NL;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.neiLi = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent("\u00A72\u00A7l\u4F60\u91CA\u653E\u4E86\uFF1A\u626B\u5251\u5F0F"), (true));
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).oneHandJian + 0.8;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.oneHandJian = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				if (entity instanceof LivingEntity _entity)
					_entity.swing(InteractionHand.MAIN_HAND, true);
				if (world instanceof ServerLevel _level) {
					Entity entityToSpawn = new LLJianQiEntity(KungFuModEntities.LL_JIAN_QI.get(), _level);
					entityToSpawn.moveTo(x, (y + 1.4), z, entity.getYRot(), entity.getXRot());
					entityToSpawn.setYBodyRot(entity.getYRot());
					entityToSpawn.setYHeadRot(entity.getYRot());
					entityToSpawn.setDeltaMovement((entity.getLookAngle().x * 8), (entity.getLookAngle().y * 8), (entity.getLookAngle().z * 8));
					if (entityToSpawn instanceof Mob _mobToSpawn)
						_mobToSpawn.finalizeSpawn(_level, world.getCurrentDifficultyAt(entityToSpawn.blockPosition()), MobSpawnType.MOB_SUMMONED,
								null, null);
					world.addFreshEntity(entityToSpawn);
				}
				if (world instanceof Level _level) {
					if (!_level.isClientSide()) {
						_level.playSound(null, new BlockPos(x, y, z),
								ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.player.attack.sweep")), SoundSource.PLAYERS,
								(float) 1.2, 3);
					} else {
						_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.player.attack.sweep")),
								SoundSource.PLAYERS, (float) 1.2, 3, false);
					}
				}
				{
					final Vec3 _center = new Vec3((entity.getLookAngle().x * 3 + x), (entity.getLookAngle().y * 3 + y),
							(entity.getLookAngle().z * 3 + z));
					List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(6 / 2d), e -> true).stream()
							.sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).collect(Collectors.toList());
					for (Entity entityiterator : _entfound) {
						if (!(entityiterator == entity)) {
							if (entityiterator instanceof LivingEntity _entity)
								_entity.hurt(new EntityDamageSource("saojianshi", entity),
										(float) (5.5 + (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
												.orElse(new KungFuModVariables.PlayerVariables())).neiLiMax
												* KungFuModVariables.MapVariables.get(world).neiLiMul));
						}
					}
				}
			} else {
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent("\u00A7c\u00A7l\u5185\u529B\u4E0D\u8DB3\uFF01"), (true));
			}
		} else {
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(new TextComponent("\u00A7c\u00A7l\u4F60\u91CA\u653E\u7684\u62DB\u5F0F\u5C1A\u672A\u5B66\u4E60\uFF01"),
						(true));
		}
	}
}
